package org.tmad.ocms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import org.tmad.ocms.log.LogFactory;
import org.tmad.ocms.model.Case;
import org.tmad.ocms.model.Volunteer;
import org.tmad.ocms.util.CommonUtils;

public class LoginDAO {
	
	private static Logger logger = LogFactory.getLogger();
	public boolean login(String email, String pwd) throws Exception{
		boolean result =false;
		
		Connection con =ConnectionManager.getDbConnection();
		Statement stmt =con.createStatement();
		ResultSet results =stmt.executeQuery("select * from volunteer where email_id='" + email + "' and password ='" + CommonUtils.getEncryptedPassword(pwd) +"'");
		//ResultSet results =stmt.executeQuery("select * from user where username='" + username + "'");
		while(results.next()){
			result = true;
		}
		return result;
	}

	public boolean resetPassword(String userName, String password) {
		
		return true;
	}
	@SuppressWarnings("deprecation")
	public Volunteer getUserByEmail(String email) {
		Volunteer user = null;
		try{
		Connection con =ConnectionManager.getDbConnection();
		Statement stmt =con.createStatement();
		ResultSet results =stmt.executeQuery("select * from volunteer where email_id='" + email + "'");
		//ResultSet results =stmt.executeQuery("select * from user where username='" + username + "'");
		
		while(results.next()){
			user = new Volunteer(results.getString("email_id"),results.getString("first_name"),results.getString("last_name"), null, null);
		}
		}catch(SQLException e){
			logger.error("jdbc exception", e);
			
		} catch (Exception e) {
			
			logger.error("unknown exception", e);
		}
		return user;

		
	}
	
	public boolean caseregister(Case username) throws Exception {
		Connection con = ConnectionManager.getDbConnection();
		PreparedStatement stmt = con
				.prepareStatement("insert into casereg(case_category_id,help_category_id , case_summary,case_description,postal_code,city,address,state_id,person_name,contact_mobile,contact_phone,contact_email,case_status_id) values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
		
		stmt.setString(1, username.getCaseType());
		stmt.setString(2, username.getHelpType());
		stmt.setString(3, username.getSummary());
		stmt.setString(4, username.getDescription());
		stmt.setString(5, username.getNeedyName());
		stmt.setString(6, username.getCity());
		stmt.setString(7, username.getAddress());		stmt.setString(8, username.getState());
		stmt.setString(9, username.getPostalCode());
		stmt.setString(10, username.getMobile());
		stmt.setString(11, username.getContactPhone());
		stmt.setString(12, username.getEmailId());
		stmt.setInt(13, username.getStatusId());
		
				return stmt.execute();

	}

	public static void main(String[] args) throws Exception {
		LoginDAO dao = new LoginDAO();
		boolean result =dao.login("harinath","secret");
		
		logger.info("user authentication status  " + result);
	}

	


}
